package com.zkld.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Controller
@RequestMapping("/pictures")
@CrossOrigin("*")
public class PictureController {
    @GetMapping
    public void getPicture(HttpServletResponse resp, @RequestParam("picturePath") String picturePath) throws IOException {
        InputStream in = new FileInputStream(new File("D:/pictures/" + picturePath));
        OutputStream out = resp.getOutputStream();
        resp.setContentType("application/octet-stream");
        resp.addHeader("Content-Disposition", "attachment; filename=\"" + picturePath + "\"");
        IOUtils.copy(in, out);
        out.flush();
        out.close();
        in.close();
    }
}
